//== GAIA ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"

#include "sagittarius/unicode/CUnicode.h"
#include "sagittarius/lexer/lexx.h"

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define N 6

char* a[N] = {"a", "b", "c", "d", "e", "f"};

void print_order(int order[N]) {
    int i;
    printf("order = {");
    for(i=0; i<N-1; ++i)
        printf("%d, ", order[i]);
    printf("%d}\n", order[N-1]);
}

void permute(int x, int y, int order[N]) {
    int tmp = order[x];
    AT(x < N && y < N, "premute range exceeded");
    order[x] = order[y];
    order[y] = tmp;
}

void print_ws(int n) {
    int i;
    for(i=0; i<n; ++i)
        printf(" ");
}

void permuteS(int x, int n, int order[N]) {
    int i;
    AT(n > 0);
    AT(x >= 0 && x < N);
    printf("permuteS %d\n", n);
    if(n == 2)
        --n;
    
    for(i=0; i<n; ++i) {
        if(n > 2)
            permuteS(x+1, n-1, order);
        permute(x, x+1, order);
        
        print_ws(N-n);
        printf("else if %s < %s\n", a[order[x]], a[order[x+1]]);
        print_order(order);
    }
}

void unravel(int x, int y, int order[N]) {
    //printf("if(%s <= %s)", a[x], a[y]);
    //if(order[x] < order[y])
    //print_order(order);
    if(x+1 < N && y+1 < N-1)
        unravel(order[x+1], order[y+1], order);
    permuteS(x, N-x, order);
}

int main()
{
    int order[N] = {0, 1, 2, 3, 4, 5};
    //unravel(0, 1, order);
    permuteS(3, 3, order);
    
    return 0;
}
